package Structural.Flyweight.travel;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

//享元工厂角色
//行程工厂类
public class TravelFactory {
    private static Map<String, Travel> sTravelMap = new ConcurrentHashMap<>();

    public static Travel getTravel(String from, String to) {
        String key = from + "-" + to;
        if (sTravelMap.containsKey(key)) {
            System.out.println("使用缓存==>" + key);
            return sTravelMap.get(key);
        } else {
            System.out.println("创建对象==>" + key);
            Travel travel = new CrhTravel(from, to);
            sTravelMap.put(key, travel);
            return travel;
        }
    }
}

